package com.inetools.gwetons.client;

import java.util.LinkedHashSet;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

public class MetaTagCrawler {
	/**
	 * @param metaName to specify value of name attribute of meta tag. It's case sensitive.
	 * @return value(s) of content attribute of meta tag(s). If name and content attributes are not 
	 * typed with lower cases, then this will not pick up that meta tag. 
	 * i.e. wrong: <meta Name="testName" Content="testContent" />, right: <meta name="testName" content="testContent" />
	 * @throws IllegalArgumentException
	 */
	static public LinkedHashSet<String> getContents( String metaName) throws IllegalArgumentException {
		if ( ( metaName == null) || "".equals( metaName)) {
			throw new IllegalArgumentException( "");
		}

		LinkedHashSet<String> contentLinkedHashSet = new LinkedHashSet<String>();
		
		NodeList<Element> metaTagNodeList = Document.get().getElementsByTagName( "meta");
		for( int metaTagIndex = 0; metaTagIndex < metaTagNodeList.getLength(); metaTagIndex++) {
			Element metaTagElement = metaTagNodeList.getItem( metaTagIndex);
			if ( !metaName.equals( metaTagElement.getAttribute( "name"))) continue; // for
			String content = metaTagElement.getAttribute( "content");
			if ( ( content == null) || "".equals( content))continue; // for
			contentLinkedHashSet.add( content);
		} // for
		
		return contentLinkedHashSet;
	} // static public LinkedHashSet<String> getContents( String metaName) throws IllegalArgumentException
}
